﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace ARelogRelog
{
    public class ARelog
    {
        List<Schedule> _listSchedules;
        List<ARelogMonitor> _listMonitors;

        public ARelog(string fn)
        {
            FileName = fn;
        }

        public string FileName
        {
            get;
            private set;
        }

        public List<ARelogMonitor> Monitors
        {
            get
            {
                if (_listMonitors == null)
                {
                    _listMonitors = new List<ARelogMonitor>();

                    XmlDocument xdMonitors = new XmlDocument();
                    xdMonitors.Load(Path.Combine(Path.GetDirectoryName(FileName), "Monitors.xml"));


                    foreach (XmlElement xeMonitor in xdMonitors.SelectNodes("Monitors/Monitor"))
                    {
                        if (xeMonitor.SelectSingleNode("BuddyPath").InnerText.Length > 0 && xeMonitor.SelectSingleNode("GamePath").InnerText.Length > 0)
                        {
                            _listMonitors.Add(new ARelogMonitor(
                                xeMonitor.SelectSingleNode("GamePath").InnerText,
                                xeMonitor.SelectSingleNode("BuddyPath").InnerText,
                                xeMonitor.SelectSingleNode("Enabled").InnerText == "true"));
                        }
                    }
                }

                return _listMonitors;
            }
        }

        public List<Schedule> Schedules
        {
            get
            {
                if (_listSchedules == null)
                    _listSchedules = Config.LoadARelogSchedules(this);

                return _listSchedules;
            }
        }

        public void SetSchedules(List<Schedule> list)
        {
            _listSchedules = list;
            Config.SaveARelogSchedules(this);
        }

        public bool ShouldRun
        {
            get
            {
                foreach (Schedule s in Schedules)
                {
                    DateTime start = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, s.Start.Hour, s.Start.Minute, s.Start.Second);
                    DateTime end = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, s.End.Hour, s.End.Minute, s.End.Second);

                    if (start <= DateTime.Now && end > DateTime.Now)
                        return true;
                }

                return false;
            }
        }
    }
}
